package com.DAO.Impl;

import com.DAO.ClassTypeDAO;
import com.DBUtil.DBUtil;
import com.entity.Class;
import com.entity.ClassType;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 20x
 * @date 2024 - 12 -02
 **/
public class ClassTypeImpl implements ClassTypeDAO{

    @Override
    public List<String> listClassType() {
        List<String> list = new ArrayList<>();
        String sql = "select typeName from ClassType";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            ResultSet rst = pst.executeQuery();
            System.out.println(sql);
            while (rst.next()){
                String typeName = rst.getString(1);
                list.add(typeName);
            }
            DBUtil.close(rst,pst,conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int AddClassType(String classType, String classDescription) {
        int i = 0;
        String sql = "insert into ClassType(typeName,typedescription) values(?,?)";
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1,classType);
            pst.setString(2,classDescription);
             i = pst.executeUpdate();
            DBUtil.close(null,pst,conn);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return i;
    }

    public static void main(String[] args) {
        ClassTypeDAO classTypeDAO = new ClassTypeImpl();
//        List<String> list = classTypeDAO.listClassType();
//        System.out.println(list);

//        int i = classTypeDAO.AddClassType("网球","网球是一项非常受欢迎的运动项目，它需要运动员具备良好的身体素质和技巧。");
//        System.out.println(i);
    }
}
